/**
 * controller
 * @return
 */
module.exports = Controller("Home/BaseController", function(){
    "use strict";
    var fs = require("fs"),
        rimraf = require("rimraf"),
        mkdirp = require("mkdirp");
    return {
        indexAction: function(http){
            console.log(process.cwd());
            this.display();
        },
        uploadFileAction: function() {
            var self = this;
            var fileInfo = this.file('qqfile');
            // paths/constants
            var assetsPath = process.cwd() + "/resource/",
            placeholdersPath = assetsPath + "placeholders/",
            uploadedFilesPath = assetsPath + "uploadedFiles/",
            chunkDirName = "chunks",
            maxFileSize = 0; // in bytes, 0 for unlimited
            function isValid(size) {
                return maxFileSize === 0 || size < maxFileSize;
            }
            function failWithTooBigFile(responseData) {
                responseData.error = "Too big!";
                self.json(responseData);
            }
            function moveUploadedFile(fileInfo,success, failure) {
                var destinationDir = uploadedFilesPath,
                    fileDestination = destinationDir + fileInfo.originalFilename;

                moveFile(destinationDir, fileInfo.path, fileDestination, success, failure);
            }
            function moveFile(destinationDir, sourceFile, destinationFile, success, failure) {
                mkdirp(destinationDir, function(error) {
                    var sourceStream, destStream;

                    if (error) {
                        console.error("Problem creating directory " + destinationDir + ": " + error);
                        failure();
                    }
                    else {
                        sourceStream = fs.createReadStream(sourceFile);
                        destStream = fs.createWriteStream(destinationFile);
                        console.log(sourceFile,destinationFile);
                        sourceStream
                            .on("error", function(error) {
                                console.error("Problem copying file: " + error.stack);
                                destStream.end();
                                failure();
                            })
                            .on("end", function(){
                                destStream.end();
                                success();
                            })
                            .pipe(destStream);
                    }
                });
            }
            function onSimpleUpload(fileInfo) {
                var responseData = {
                        error: 0,
                        errmsg: 'ok',
                        success: false, //只有success返回true才认为上传成功
                        filepath : "/resource/uploadedFiles/" + fileInfo.originalFilename
                    };


                if (isValid(fileInfo.size)) {
                    moveUploadedFile(fileInfo, function() {
                            responseData.success = true;
                            self.json(responseData);
                        },
                        function() {
                            responseData.error = "Problem copying the file!";
                            self.json(responseData);
                        });
                }
                else {
                    failWithTooBigFile(responseData, res);
                }
            }

            if(fileInfo) {
                onSimpleUpload(fileInfo);
            }else {
                self.error();
            }
        }
    };
});